package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.warehouse.manage.dao.NoticeMapper;
import com.zhijian.warehouse.manage.entity.po.File;
import com.zhijian.warehouse.manage.entity.po.Notice;
import com.zhijian.warehouse.manage.entity.vo.request.FileSearchVo;
import com.zhijian.warehouse.manage.entity.vo.request.NoticeSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.NoticeSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.FileVo;
import com.zhijian.warehouse.manage.entity.vo.response.NoticeVo;
import com.zhijian.warehouse.manage.enums.FileEnum;
import com.zhijian.warehouse.manage.service.FileService;
import com.zhijian.warehouse.manage.service.NoticeService;

import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 公告业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-03-18 21:00:31
 */
@Slf4j
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper mapper;

    @Autowired
    private FileService fileService;

    @Override
    public Notice selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public NoticeVo selectVoById(Long id) {
        NoticeVo noticeVo = mapper.selectVoById(id);
        if (Objects.isNull(noticeVo)) {
            return null;
        }
        List<FileVo> fileVos = fileService.selectList(FileSearchVo.builder()
                .dataId(id)
                .type(FileEnum.TypeEnum.NOTICE.getCode().byteValue())
                .build());
        if (CollectionUtil.isNotEmpty(fileVos)) {
            noticeVo.setFilePathList(fileVos.stream().map(FileVo::getFilePath).collect(Collectors.toList()));
        } else {
            noticeVo.setFilePathList(Collections.emptyList());
        }
        return noticeVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(Notice notice) {
        Date now = new Date();
        notice.setCreateTime(now);
        notice.setUpdateTime(now);
        return mapper.insert(notice) > 0 ? notice.getId() : 0L;
    }

    @Override
    public boolean update(Notice notice) {
        Date now = new Date();
        notice.setUpdateTime(now);
        return mapper.updateByPrimaryKey(notice) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Notice record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(NoticeSaveOrUpdateVo saveOrUpdateVo) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(saveOrUpdateVo, notice);
        Long id = this.insert(notice);
        Date now = new Date();
        fileService.insertBatch(saveOrUpdateVo.getFilePathList().stream().map(g -> {
            File file = new File();
            file.setFilePath(g);
            file.setFileName(g);
            file.setDataId(id);
            file.setType(FileEnum.TypeEnum.NOTICE.getCode().byteValue());
            file.setCreateTime(now);
            file.setUpdateTime(now);
            return file;
        }).collect(Collectors.toList()));
        return id != 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean modify(NoticeSaveOrUpdateVo saveOrUpdateVo) {
        Notice old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        old.setTitle(saveOrUpdateVo.getTitle());
        old.setContent(saveOrUpdateVo.getContent());
        old.setWeatherShow(saveOrUpdateVo.getWeatherShow());
        fileService.deleteByCondition(FileSearchVo.builder()
                .dataId(old.getId())
                .type(FileEnum.TypeEnum.NOTICE.getCode().byteValue())
                .build());
        Date now = new Date();
        fileService.insertBatch(saveOrUpdateVo.getFilePathList().stream().map(g -> {
            File file = new File();
            file.setFilePath(g);
            file.setFileName(g);
            file.setDataId(saveOrUpdateVo.getId());
            file.setType(FileEnum.TypeEnum.NOTICE.getCode().byteValue());
            file.setCreateTime(now);
            file.setUpdateTime(now);
            return file;
        }).collect(Collectors.toList()));
        return this.update(old);
    }

    @Override
    public NoticeVo selectOne(NoticeSearchVo noticeSearchVo) {
        return mapper.selectOne(noticeSearchVo);
    }

    @Override
    public Integer count(NoticeSearchVo noticeSearchVo) {
        return mapper.count(noticeSearchVo);
    }

    @Override
    public List<NoticeVo> selectList(NoticeSearchVo noticeSearchVo) {
        List<NoticeVo> noticeVos = mapper.selectList(noticeSearchVo);
        if (CollectionUtil.isEmpty(noticeVos)) {
            return Collections.emptyList();
        }
        for (NoticeVo noticeVo : noticeVos) {
            List<FileVo> fileVos = fileService.selectList(FileSearchVo.builder()
                    .dataId(noticeVo.getId())
                    .type(FileEnum.TypeEnum.NOTICE.getCode().byteValue())
                    .build());
            if (CollectionUtil.isNotEmpty(fileVos)) {
                noticeVo.setFilePathList(fileVos.stream().map(FileVo::getFilePath).collect(Collectors.toList()));
            } else {
                noticeVo.setFilePathList(Collections.emptyList());
            }
        }
        return noticeVos;
    }

    @Override
    public Page<NoticeVo> selectPage(NoticeSearchVo noticeSearchVo) {
        AssertCommonUtil.check(Objects.isNull(noticeSearchVo.getPageNum()) || Objects.isNull(noticeSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(noticeSearchVo);
        //分页
        Page<NoticeVo> page = new Page<>(true, count, noticeSearchVo.getPageNum(), noticeSearchVo.getPageSize());
        noticeSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<NoticeVo> responseVos = this.selectList(noticeSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<Notice> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }


}